package com.example.zjx8969.myapplication.view

import android.content.Context
import android.util.TypedValue
import android.widget.Space
import android.widget.TableLayout
import android.widget.TableRow
import androidx.appcompat.widget.AppCompatImageView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.ContextCompat
import com.example.zjx8969.myapplication.DensityUtil
import com.example.zjx8969.myapplication.R
import kotlin.math.roundToInt

fun addItems(
        vipFeatureIntroduceContainer: TableLayout,
        desArray: Array<String?>
) {
    val titleImageId = "vip_subscribe_names_image".hashCode()
    val titleTextId = "vip_subscribe_names_text".hashCode()


    fun createItemView(context: Context, titleText: String?, isOnRight: Boolean) = ConstraintLayout(context).apply {
        val imageView = AppCompatImageView(context).apply {
            id = titleImageId
            layoutParams = ConstraintLayout.LayoutParams(DensityUtil.dp2px(9.6f).roundToInt(), ConstraintLayout.LayoutParams.WRAP_CONTENT).apply {
                left = ConstraintLayout.LayoutParams.PARENT_ID
                topToTop = ConstraintLayout.LayoutParams.PARENT_ID
                bottomToBottom = ConstraintLayout.LayoutParams.PARENT_ID
            }
            setImageResource(R.drawable.ic_feature_right)
        }
        addView(imageView)

        val text = AutofitTextView(context).apply {
            id = titleTextId
            layoutParams = ConstraintLayout.LayoutParams(DensityUtil.dip2px(context, 121f), ConstraintLayout.LayoutParams.WRAP_CONTENT).apply {
                val marginStart = DensityUtil.dip2px(context, 9f)
                val marginTop = DensityUtil.dip2px(context, 3f)
                setMargins(marginStart, marginTop, marginTop, 0)
                leftToRight = titleImageId
                topToTop = ConstraintLayout.LayoutParams.PARENT_ID
                bottomToBottom = ConstraintLayout.LayoutParams.PARENT_ID
            }
            maxLines = 2
            setTextSize(TypedValue.COMPLEX_UNIT_SP, 11f)
            setTextColor(Color(0xFF3b3b3b).toIntColor())
            text = titleText
        }
        addView(text)

        if (!isOnRight) Space(context).apply {
            layoutParams = ConstraintLayout.LayoutParams(DensityUtil.dip2px(context, 27.84f), ConstraintLayout.LayoutParams.WRAP_CONTENT).apply {
                leftToRight = titleTextId
                topToTop = ConstraintLayout.LayoutParams.PARENT_ID
                bottomToBottom = ConstraintLayout.LayoutParams.PARENT_ID
            }
            addView(this)
        }
        setBackgroundColor(ContextCompat.getColor(context, R.color.colorAccent))
    }

    for (i in desArray.indices step 2) {
        TableRow(vipFeatureIntroduceContainer.context).apply {
            layoutParams = TableRow.LayoutParams()
            addView(createItemView(context, desArray[i], false))
            if (i + 1 < desArray.size) addView(createItemView(context, desArray[i + 1], true))
            vipFeatureIntroduceContainer.addView(this)
        }

    }
}

@JvmInline
value class Color(val colorValue: Long)
fun Color.toIntColor(): Int = (colorValue and 0xFFFFFFFF).toInt()


